﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	//负责格式化文本框.
	class FormFormatTextBoxWorker
	{
        //CID 1339 //2012.09.18 //Begin
        public static void formatRichText(Form form, XmlElement spanElement, FormatInfo info)
        {
            if (!info.isIE)
            {
                string style = spanElement.GetAttribute("style");
                if (style != null && style.Length > 0)
                {
                    style = style.Replace("; HEIGHT:", "; MIN-HEIGHT:");
                    spanElement.SetAttribute("style", style);
                }
            }
        }
        //CID 1339 //2012.09.18 //End

        //CID 0827 //2011.03.07 //Begin
        public static void formatInputText(Form form, XmlElement inputTextElement, string xdBinding, string datafmtName)
        {
            string id = inputTextElement.GetAttribute(JetConstant.HTML_id);
            //CID 0230 //2009.02.25 //id.IndexOf("_") == 0
            if (id != null && id.IndexOf("_") == 0)
            {
                formatInputText(inputTextElement, datafmtName);
            }
            else
            {
                string binding = inputTextElement.GetAttribute(xdBinding);
                if (binding != null && binding.Length > 0)
                {
                    formatInputText(inputTextElement, datafmtName);
                }
            }
        }
        //CID 0827 //2011.03.07 //End
	
		//格式化TextField.
        private static void formatInputText(XmlElement inputTextElement, string datafmtName)
        {
            //CID 0642 //2010.08.19 //Begin
            string dataFmt = inputTextElement.GetAttribute(datafmtName);
            if (dataFmt.IndexOf("\"plainMultiline\"") > 0)
            {
                inputTextElement = DOMUtil.renameElementName(inputTextElement, JetConstant.HTML_textarea);
            }
            else
            {
                string style = inputTextElement.GetAttribute("style");
                if (style.IndexOf("WHITE-SPACE: normal") >= 0)
                {
                    //WHITE-SPACE: normal表示文本框自动折行,将这种文本框转换为TextArea,以便能够折行.
                    inputTextElement = DOMUtil.renameElementName(inputTextElement, JetConstant.HTML_textarea);
                    inputTextElement.SetAttribute("rows", "1");
                    inputTextElement.SetAttribute("default_rows", "1");
                }
                else
                {
                    //如果只是一个单行TextField,将元素内的文本移动为value属性.
                    inputTextElement.SetAttribute(JetConstant.HTML_value, inputTextElement.InnerText);
                    DOMUtil.resetText(inputTextElement, "");
                }
            }
            //CID 0642 //2010.08.19 //End
		}
	
		//格式化TextArea.
        //CID 0827 //2011.03.07 //public
		public static void formatTextArea( Form form, XmlElement textAreaElement, string datafmtName, string xctnameName ){
			string xctname = textAreaElement.GetAttribute( xctnameName );
			formatScollingStyle( textAreaElement, xctname );
			string dataFmt = textAreaElement.GetAttribute( datafmtName );
			//默认设置为3行.
			if( dataFmt.IndexOf( "\"plainMultiline\"" ) > 0 ){
				textAreaElement.SetAttribute( "rows", "3" );
				textAreaElement.SetAttribute( "default_rows" , "3" );
			}

			if( textAreaElement.GetAttribute( "default_rows" ).Length <= 0 ){
				textAreaElement.SetAttribute( "rows" , "3" );
				textAreaElement.SetAttribute( "default_rows" , "3" );
			}

			//CID 0097 //2008.08.21 //Begin
			if( xctname.Equals( JetConstant.XCT_PlainText ) )
			{
				textAreaElement.SetAttribute( "rows" , "1" );
				//如果是Multiple-line Textbox,default_rows为3,也就是行数可扩展.
				if( dataFmt.IndexOf( "\"plainMultiline\"" ) > 0 )
				{
					textAreaElement.SetAttribute( "default_rows" , "3" );

					//CID 0161 //2008.12.07 //Begin
					//CID 0120 //2008.09.22 //Comment
					//Multiple-line Textbox需要删除formatScollingStyle添加的OVERFLOW-Y: visible;
					//所有Multiple-line Textbox都是不显示Scroll的,所以OVERFLOW设置为visible.
					/*
					string style = textAreaElement.GetAttribute( "style" );
					if( style.IndexOf( "OVERFLOW-Y: scroll;OVERFLOW-X: scroll;" ) < 0 )
					{
						style = style + ";OVERFLOW-X: visible;OVERFLOW-Y: visible;";
					}
					textAreaElement.SetAttribute( "style", style );
					*/
					//CID 0120 //2008.09.22 //Comment
					//CID 0161 //2008.12.07 //End
				}
				else
				{
					textAreaElement.SetAttribute( "default_rows" , "1" );
				}
			}

			string cssText = textAreaElement.GetAttribute( "style" );
			//如果有滚动条,去掉rows,此时,style中应该设置了height,使用height设置高度.
			if( cssText.IndexOf( "OVERFLOW-Y: scroll;OVERFLOW-X: scroll;" ) >= 0 )
			{
				textAreaElement.RemoveAttribute( "rows" );
			}
			//CID 0097 //2008.08.21 //End

            //CID 0642 //2010.08.19 //Begin
            if (cssText.IndexOf("WHITE-SPACE: nowrap") >= 0)
            {
                cssText = cssText.Replace("WHITE-SPACE: nowrap", "WHITE-SPACE: normal");
                textAreaElement.SetAttribute("style", cssText);
                textAreaElement.SetAttribute("wrap", "off");
            }
            //CID 0642 //2010.08.19 //End
		}
	
		//将ScrollingSytle改为适合Web显示的参数.
		private static void formatScollingStyle( XmlElement textAreaElement, string xctname ){
			string style = textAreaElement.GetAttribute( "style" );
			if( style.IndexOf( "OVERFLOW-Y: scroll" ) >= 0 )
			{
				textAreaElement.SetAttribute( "hasScrollBar", "true" );
			}
			/*
			if( !hasScrollBar )
			{
				//CID 0161 //2008.12.07 //Begin
				if( style.IndexOf( "OVERFLOW-Y: auto" ) < 0 && style.IndexOf( "OVERFLOW-X: visible;" ) >= 0 )
				{
					//styleBuffer.Append( "OVERFLOW-Y: visible;" );
					//CID 0120 //2008.09.22 //Comment
					style = style + ";OVERFLOW-Y: visible;";
					//CID 0120 //2008.09.22 //Comment
				}
				//CID 0161 //2008.12.07 //End
			}
			*/

			//CID 0161 //2008.12.07 //Begin
			//Onkostendeclaratie_PoC.xsn style.txt
			if( xctname.Equals( JetConstant.XCT_RichText ) )
			{
				if( style.IndexOf( "OVERFLOW-Y:" ) < 0 )
				{
					style = style + ";OVERFLOW-Y: visible;";
				}
			}
			//CID 0161 //2008.12.07 //End

			textAreaElement.SetAttribute( "style", style );

		
			//CID 0101 //2008.09.14 //Comment
			/*
			StringBuilder styleBuffer = new StringBuilder();
			StringTokenizer propTokenizer = new StringTokenizer( style, ";" );

			while( propTokenizer.HasMoreTokens() ){
				string prop = propTokenizer.NextToken();
				prop = prop.Trim();
				if( prop.StartsWith( "HEIGHT:" ) ){
					//if( hasScrollBar ){	//CID 0097 //2008.08.21 //如果显示了ScrollBar,那就保留Style高度设置.
						styleBuffer.Append( prop );
						styleBuffer.Append( ";" );	
					//}
				}else{
					styleBuffer.Append( prop );
					styleBuffer.Append( ";" );	
				}
				styleBuffer.Append( prop );
				styleBuffer.Append( ";" );	
			}
			*/
			//CID 0101 //2008.09.14 //Comment
		}
	}
}